using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Team4sCMDB
{
    public class AttributePanelCreator
    {
        public static Panel createPanel(DataTable dataTable, bool isEmpty, Main main)
        {
            main.AttributeTextBoxes.Clear();

            Panel panel = new Panel();
            panel.AutoSize = true;
            
            //panel.Size = new System.Drawing.Size(100, 400);
            //panel.Location = new System.Drawing.Point(500, 30);

            TableLayoutPanel tlp = new TableLayoutPanel();

            panel.Controls.Add(tlp);
            tlp.Dock = DockStyle.Fill;
            tlp.AutoSize = true;

            if (isEmpty)
            {
                // Create button for savint the new Ci
                Button saveNewCi = new Button();
                saveNewCi.AutoSize = true;
                saveNewCi.Text = "Opslaan";
                saveNewCi.Click += new EventHandler(main.saveNewCi_Click);
                tlp.Controls.Add(saveNewCi, 0, 0);
            }
            else
            {
                // Create button for editing the Ci
                Button editCi = new Button();
                editCi.AutoSize = true;
                editCi.Text = "Wijzigingen opslaan";
                editCi.Click += new EventHandler(main.editCi_Click);
                tlp.Controls.Add(editCi, 0, 0);
            }


            // +1 or -1 for offset of Save buton
            int index = 1;
            int indexTable = 1;
            //for (int i = 1; i < dataTable.Columns.Count+1; i++)
            //{
            foreach(DataColumn column in dataTable.Columns)
            {
                //check if data is relevant
                if (column.ColumnName != "StatusNr" && column.ColumnName != "Locatienr" && column.ColumnName.IndexOf(".CInr") == -1)
                {
                    Label lbl = new Label();
                    tlp.Controls.Add(lbl, 0, index);
                    // lbl.Location = new System.Drawing.Point(5, i * (2 * lbl.Size.Height + 3));
                    lbl.Text = dataTable.Columns[indexTable - 1].ColumnName;
                    TextBox tb = new TextBox();
                    tlp.Controls.Add(tb, 1, index);
                    //tb.Location = new System.Drawing.Point(5, (i * (2 * lbl.Size.Height + 3)) + lbl.Size.Height + 3);

                    // For filling the table
                    if (!isEmpty)
                    {
                        tb.Text = dataTable.Rows[0][indexTable - 1].ToString();
                    }

                    main.AttributeTextBoxes.Add(dataTable.Columns[indexTable - 1].ColumnName, tb);
                    index++;

                }
                indexTable++;
            }
            
            return panel;
            
            
        }

        private bool checkAuth(string userid) 
        {
            if (true) // sql check user authorization
            {
                return true;
            }
            else 
            { 
                return false; 
            }
        }
    }
}